<?php
class Inbox
{
	public $id;
    public $senderID;
    public $recipientID;
	public $msgdate;
	public $body;
	public $subject;
    public $seen;
	

	public function __construct($db,$id)
    {
		$reponse = $db->query('SELECT * FROM jktrade_inbox WHERE id='.$id.'');
		$data = $reponse->fetch();
		
		$this->id=$id;
		$this->senderID=$data['senderID'];
		$this->recipientID=$data['recipientID'];
		$this->msgdate=$data['date'];
		$this->body=$data['body'];
		$this->subject=$data['subject'];
		$this->seen=$data['seen'];
		
	}
	
	


		public function markasread($db)
{
		$db->exec('UPDATE jktrade_inbox SET seen = "yes" WHERE id = '.$this->id.'');
}
	

}

///////////////////////////////////////////////// OUT OF CLASS FUNCTIONS

function inboxsend($db, $senderID, $recipientID, $body, $subject)
		{
		// Checking database connection //
				try
				{ $db; }
				catch(Exception $e) //if there's some trouble
				{ die('Error connecting to the database : '.$e->getMessage()); }
		
		// Database connected. Let's process //
		
				$req = $db->prepare('INSERT INTO jktrade_inbox(senderID, recipientID, date, body, subject, seen) 
												VALUES(:senderID, :recipientID, :msgdate, :body, :subject, :seen)');
				$req->execute(array(
					'senderID' => $senderID,
					'recipientID' => $recipientID,
					'msgdate' => date("Y-m-d H:i:s"),
					'body' => $body,
					'subject' => $subject,
					'seen' => 'no' 
					));
		}


function displayinbox($db, $displayer, $type)
	{
	
	if($type=="received")
	{
	$getinbox = $db->query('SELECT * FROM jktrade_inbox WHERE recipientID="'.$displayer->id.'" ORDER BY DATE DESC');
	}
	if($type=="sent")
	{
	$getinbox = $db->query('SELECT * FROM jktrade_inbox WHERE senderID="'.$displayer->id.'" ORDER BY DATE DESC');
	}
	echo'
	
                 <table class="table table-striped">
                     <thead>
                         <tr>
                            <th>User</th>
                            <th>Subject</th>
                            <th>Date</th>
                          </tr>
                        </thead>
                        <tbody>
						';
						
						while ($messagedata = $getinbox->fetch())
				{	
						if($type=="received")
						{$user = new User($db, $messagedata['senderID']);	
						echo'
                          <tr>
                            <td>'.$user->firstname . " " .$user->surname.'</td>';}
							
						if($type=="sent")
						{$user = new User($db, $messagedata['recipientID']);
						echo'
                          <tr>
                            <td>To: '.getName($db, $displayer, $messagedata['id']).'</td>'
							;}

							
					
					echo'
                            <td><a href="account.php?page=inbox&v='.$messagedata['id'].'">'.$messagedata['subject'].'</a></td>
                            <td>'.date('l, M jS, g:i A (e)', strtotime($messagedata['date'])).'</td>
                          </tr>
					';
                }
				
				echo'
                        </tbody>
                      </table>';
					  
	
	}

function displayconversation($db, $user, $conversationID)
{

	
		$msg=new Inbox($db, $conversationID);
		if($msg->recipientID==$user->id)
		{$msg->markasread($db);}
	
		if($msg->senderID!=$user->id)//He sent the message
		{ 
	
			echo'	
		<div class="bubble_wrapper">
								<div class="bubble_left" id="" align="left">
								'.$msg->body.'
								<br/> <span style="font-size: 0.7em; color: grey;">SENT '.date('l, M jS, g:i A (e)', strtotime($msg->msgdate)).'</span></div>
				</div>';
			
		}
		else //I sent the message
		{ echo'
		<div class="bubble_wrapper">
								<div class="bubble_right"  align="right">
								'.$msg->body.'
								<br/> <span style="font-size: 0.7em; color: black;">SENT '.date('l, M jS, g:i A (e)', strtotime($msg->msgdate)).'</span></div>
				</div>';
		}
			
		echo'<div class="bubble_wrapper">
								<div class="bubble_right"  align="right">
								<form action="account.php?page=inbox" method="post">
								<input type="text" name="subject" value="RE: '.$msg->subject.'"/><br/>
								<textarea style="min-width: 350px;" name="body"></textarea>
								<input type="hidden" name="op" value="sendmsg"/>
								';
								if($msg->senderID!=$user->id)
								{
								echo'<input type="hidden" value="'.$msg->senderID.'" name="recipientID"/>';
								}
								else{
								echo'<input type="hidden" value="'.$msg->recipientID.'" name="recipientID"/>';
								}
								echo'
								<input type="hidden" value="'.$user->id.'" name="senderID"/>
								
								<br/> <input class="btn btn-primary" value="Answer" type="submit"/></form></div>
			</div><hr/>';
}


function getName($db,$user,$convID) // This function returns the name of your correspondant in a conversation.
{
$msg = new Inbox($db,$convID);
if($user->id!=$msg->senderID){$id=$msg->senderID;}
else{$id=$msg->recipientID;}
$destname=new User($db,$id);
return $destname->firstname . " " . $destname->surname;

}

function getUnreadAmount($db, $user) 
{
$req = $db->query('SELECT id FROM jktrade_inbox WHERE (seen="no" AND recipientID='.$user->id.') ORDER BY DATE DESC');
$i=0; // Number of unread message intiation
while ($found = $req->fetch())
	{
		$i++;
	}
return $i;

}



	
?>